package core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class TC001 {

  private static final boolean DETAILED = true;
  private static final String FILE = "src/core/data.txt";
  /**
   * @param args
   */
  public TC001() throws Exception {
    getTime();
    //compute();
    
  }

  private void getTime() throws Exception { 
    String[] days = {"Sobota", "Nedela", "Pondelok", "Utorok", "Streda", "Stvrtok", "Piatok"};
    ArrayList<Data> weeks = new Data().getWeeks();
    
    long total = 0;
    for (int i = 0; i < weeks.size(); i++) {
      System.out.println("WEEK " + weeks.get(i).getWeek());
      System.out.println("--------------------------------");
      long sum = 0;
      long mindSum = 0;
      //Date tmp = null;
      Calendar tmp = Calendar.getInstance();
      Calendar begin = Calendar.getInstance();
      Data w = weeks.get(i);
      for (DateInterval d : w.getData()) {
        begin.setTime(d.getBegin());
        if (DETAILED) {
          if (mindSum == 0) {
            tmp.setTime(d.getBegin());
          }
          if (tmp.get(Calendar.DAY_OF_MONTH) == begin.get(Calendar.DAY_OF_MONTH)) {
            mindSum += d.getEnd().getTime() - d.getBegin().getTime();
          } else {
            //System.out.format("%2d.%1d %9s: %6s\n", tmp.getDate(), tmp.getMonth(), days[tmp.getDay()], getHMS(mindSum));
            System.out.println(tmp.get(Calendar.DAY_OF_MONTH) + "." + (tmp.get(Calendar.MONTH) + 1) + "  " + days[tmp.get(Calendar.DAY_OF_WEEK)] + ": " + getHMS(mindSum));
            tmp.setTime(begin.getTime());
            mindSum = d.getEnd().getTime() - d.getBegin().getTime();
          }
        }
        long diff = d.getEnd().getTime() - d.getBegin().getTime();
        sum += diff;
        total += diff;
      }
      if (DETAILED) {
        System.out.println(tmp.get(Calendar.DAY_OF_MONTH) + "." + (tmp.get(Calendar.MONTH) + 1) + "  " + days[tmp.get(Calendar.DAY_OF_WEEK)] + ": " + getHMS(mindSum));
        System.out.println("--------------------------------");  
      }
      System.out.println("Time in hours: " + getHMS(sum));
      if ((60 * 60 * 1000 * 40) - sum > 0) {
        System.out.println("Need to work : " + getHMS((60 * 60 * 1000 * 40) - sum) + "\n");
      } else {
        System.out.println("Overtime : " + getHMS(sum - (60 * 60 * 1000 * 40)) + "\n");
      }
    }
    System.out.println("================================\nTotal: " + getHMS(total) + "\n================================");
  }
  
  private String getHMS(long time) {
    long hours = time / (60 * 60 * 1000);
    long minutes = (time % (60 * 60 * 1000)) / (60 * 1000);
    long seconds = (time % (60 * 60 * 1000)) % (60 * 1000) / 1000;
    
    DecimalFormat f = new DecimalFormat("00");
    return hours + ":" + f.format(minutes) + ":" + f.format(seconds);
  }
  
  private ArrayList<Data> parseData(String file) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");  
    ArrayList<Data> data = new ArrayList<>();
    
    FileReader input = new FileReader(file);
    BufferedReader bufRead = new BufferedReader(input);
    List<String> rawData = new ArrayList<>();
    String line = null;
    while ((line = bufRead.readLine()) != null) {
      rawData.add(new String(line.replaceAll("\\s+"," ")));        
    }
    bufRead.close();
    
    if ((rawData.size() % 2) != 0) {
      throw new Exception("Error during the parsing...\nThe data contains uneven row");
    }
    Calendar cal = Calendar.getInstance();
    for(int i = 0; i < (rawData.size() / 2); i++) {
      cal.setTime(format.parse(rawData.get(i * 2)));
      data.add(new Data(cal.get(Calendar.WEEK_OF_YEAR), new DateInterval(format.parse(rawData.get(i)), format.parse(rawData.get(i + 1)))));
    }
        
    return data;
  }
  
  private void compute() throws Exception {
    Calendar cal = Calendar.getInstance();
    ArrayList<Data> data = parseData(FILE);
    cal.setTime(data.get(0).getInterval().getBegin());
    
    int week = cal.get(Calendar.WEEK_OF_YEAR);
    long yearTime = 0, weekTime = 0, dayTime = 0;
    int ix = 0;
    int w = 0;
    while (week == cal.get(Calendar.WEEK_OF_YEAR)) {
      w = cal.get(Calendar.WEEK_OF_YEAR);
      weekTime += data.get(ix).getInterval().getEnd().getTime() - data.get(ix).getInterval().getBegin().getTime();
      
      ix++;
      cal.setTime(data.get(ix).getInterval().getBegin());
    }
    System.out.println("Time in hours: " + getHMS(weekTime));
  }
  
  
}
